package jt.android.platformania.api;

import jt.android.platformania.world.World;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnTouchListener;

public class GameThread extends Thread implements OnTouchListener
{
	private ThreadState state;
	private GameView gameView;
	private Handler	soundHandler;
	private	SurfaceHolder surfaceHolder;
	private World world;
	private Point input;
	private long timestamp;
	private float elapsed_time;
	private float debug_timer;
	private int debug_x;
	private int debug_y;

	public GameThread(GameView _gameView)
	{
		// Set reverse reference to GameView
		gameView = _gameView;
		input = new Point();
		
		// Set initial state
		state = ThreadState.INITIALIZED;
		
		// Initialize game data
		world = new World();
		
		// Time
		timestamp = System.currentTimeMillis();
		debug_timer = 0.0f;
		debug_x = 50;
		debug_y = 50;
	}
	
	public void run()
	{
		while(state == ThreadState.RUNNING)
		{
			// Update timing
			elapsed_time = (System.currentTimeMillis() - timestamp)/1000.0f;
			timestamp = System.currentTimeMillis();
			debug_timer -= elapsed_time;
			if (debug_timer < 0)
			{
				debug_timer = 5.0f;
				playSound(SoundManager.SND_TEST);
			}
			
			// Check game state
			
			// Update input
			// input = gameView.getPrevInput();
			debug_x = input.x;
			debug_y = input.y;
			
			// Update world
			
			// Render world
			render();
		}
	}
	
	public void registerSoundHandler(Handler _soundHandler)
	{
		soundHandler = _soundHandler;
	}
	
	public void registerSurfaceHolder(SurfaceHolder _surfaceHolder)
	{
		surfaceHolder = _surfaceHolder;
	}

	public void setRunning()
	{
		state = ThreadState.RUNNING;
	}
	
	public void setPaused()
	{
		state = ThreadState.PAUSED;
	}
	
	public void setTerminated()
	{
		state = ThreadState.TERMINATED;
	}
	
	public void playSound(int sound)
	{
		// Signal application thread to say a sound via a handler message
		Message msg = new Message();
		msg.what = SoundManager.SIG_PLAY;
		msg.arg1 = SoundManager.SND_TEST;
		soundHandler.sendMessage(msg);
	}
	
	/// Draw scene in a thread-safe manner
	private void render()
	{
		Canvas canvas = null;
		try
		{
			// Retrieve canvas handle
			canvas = surfaceHolder.lockCanvas();
			synchronized(surfaceHolder)
			{
				// Pass canvas to world to be modified
				world.render(canvas);
				
				// DEBUG
				Paint p = new Paint();
				p.setARGB(255, 0, 0, 255);
				canvas.drawRect(debug_x, debug_y, debug_x+50, debug_y+50, p);
			}
		}
		finally
		{
			if (canvas != null)
			{
				// Release canvas
				surfaceHolder.unlockCanvasAndPost(canvas);
			}
		}
	}


	public boolean onTouch(View view, MotionEvent event) 
	{
		// Update input state.  This will be read in the game thread
		input.x = (int)event.getX();
		input.y = (int)event.getY();
		return true;
	}
}
